package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []int{10, 40, 28, 280}
	b := []int{234, 5, 3, 148, 23, 14}
	v := 42
	fmt.Println(isFounded(a, b, v))
}

type fastSearch []int

func (f fastSearch) Len() int {
	return len(f)
}

func (x fastSearch) Swap(i, j int) {
	x[i], x[j] = x[j], x[i]
}

func (x fastSearch) Less(i, j int) bool {
	return x[i] < x[j]
}

func isFounded(a, b []int, v int) bool {
	sort.Sort(fastSearch(b)) //先对b进行一次快排
	for _, value := range a {
		another := v - value
		//使用二分查找法加快查询速度
		index := sort.Search(len(b), func(i int) bool {
			return b[i] >= another
		})

		//如果找到，直接返回
		if b[index] == another {
			return true
		}
	}
	return false //找到最后, 都没有找到返回false
}
